package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.enums.ListTypeEnum;
import com.moli.iov.admin.model.vo.req.AlarmHandlingVo;
import com.moli.iov.admin.model.vo.req.AlarmInfoHandlerRecordReqVo;
import com.moli.iov.admin.model.vo.req.AlarmVo;
import com.moli.iov.admin.service.IAlarmFacadeService;
import com.moli.iov.alarm.model.dto.req.AlarmHandlingDto;
import com.moli.iov.alarm.model.dto.req.AlarmInfoHandlerRecordReqDto;
import com.moli.iov.alarm.model.dto.req.AlarmInfoReqDto;
import com.moli.iov.alarm.model.dto.req.AlarmMeAuditReqDto;
import com.moli.iov.alarm.model.dto.res.AlarmInfoResDto;
import com.moli.iov.alarm.remote.IAlarmRemoteService;
import com.moli.iov.handle.alarm.enums.AlarmCodeEnum;
import com.moli.iov.handle.alarm.enums.AlarmStatusEnum;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 告警管理
 * </p>
 *
 * @author zql
 * @since 2019/5/27 14:05
 */
@Slf4j
@Service
public class AlarmFacadeServiceImpl implements IAlarmFacadeService {

    @Reference(group = "${service.group}", check = false)
    private IAlarmRemoteService alarmRemoteService;

    /**
     * 报警列表
     *
     * @param alarmVo
     * @return
     */
    @Override
    public PageExtend<AlarmInfoResDto> listPageAlarmInfo(AlarmVo alarmVo) {
        AlarmInfoReqDto alarmInfoReqDto = convertToAlarmInfoReqDto(alarmVo);

        if (alarmVo.getAlarmCode() == null) {
            alarmInfoReqDto =
                    setConditionCollection(alarmVo.getListType(), alarmInfoReqDto);
        }

        if (alarmInfoReqDto.getStatus() == null) {
            List<Integer> statues = Arrays.asList(AlarmStatusEnum.ALARMING.getFlag(), AlarmStatusEnum.ALARM_END.getFlag(), AlarmStatusEnum.IGNORE.getFlag());
            alarmInfoReqDto.setStatues(statues);
        }
        log.info("报警列表 AlarmInfoDto入参:{}", alarmInfoReqDto);
        return alarmRemoteService.listPageAlarmInfo(alarmInfoReqDto);
    }

    /**
     * 设置基础条件
     *
     * @param listType
     * @param alarmInfoReqDto
     * @return
     */
    private AlarmInfoReqDto setConditionCollection(int listType, AlarmInfoReqDto alarmInfoReqDto) {
        if (ListTypeEnum.OFFLINE.getFlag() == listType) {

            alarmInfoReqDto.setAlarmCodes(Arrays.asList(
                    AlarmCodeEnum.OFF_LINE.getFlag()));

        } else if (ListTypeEnum.TERMINAL.getFlag() == listType) {

            alarmInfoReqDto.setAlarmCodes(AlarmCodeEnum.getTerminalAlarms());
        } else if (ListTypeEnum.FENCE.getFlag() == listType) {
            alarmInfoReqDto.setAlarmCodes(Arrays.asList(
                    AlarmCodeEnum.IN_FENCE.getFlag(),
                    AlarmCodeEnum.OUT_FENCE.getFlag()));
        } else {
            log.error("[报警列表] [设置基础条件] 失败 未定义列表类型 listType：{}", listType);
        }

        return alarmInfoReqDto;
    }

    /**
     * 报警详情  电子围栏报警
     *
     * @param alarmVo
     * @return
     */
    @Override
    public RestResponse<AlarmInfoResDto> getAlarmInfo(AlarmVo alarmVo) {

        log.info("报警详情 AlarmInfoDto入参:{}", alarmVo);
        return alarmRemoteService.getAlarmInfo(convertToAlarmInfoReqDto(alarmVo));
    }

    /**
     * 忽略报警
     *
     * @param alarmVo
     * @return
     */
    @Override
    public RestResponse ignore(AlarmVo alarmVo) {
        return alarmRemoteService.ignore(convertToAlarmInfoReqDto(alarmVo));
    }

    /**
     * 车辆失联报警导出
     *
     * @param alarmVo
     * @param request
     * @param res
     */
    @Override
    public void offLineExport(AlarmVo alarmVo, HttpServletRequest request,
                              HttpServletResponse res) throws Exception {
        alarmVo.setPageNum(Constants.EXPORT_PAGE_NUM);
        alarmVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
        PageExtend<AlarmInfoResDto> alarmInfoDtoPageExtend = listPageAlarmInfo(alarmVo);

        final String[] propNames = {"vin", "license", "vehicleModelStr", "alarmName", "memo", "statusName",
                "beginTime"};
        final String[] headStrs = {"车架号", "车牌号", "车型", "报警类型", "报警内容", "报警状态", "报警时间"};
        String title = "车辆失联报警";
        ExcelUtil.exportDataExcel(request, res, alarmInfoDtoPageExtend.getRecords(), propNames, headStrs, title);
    }

    /**
     * 车载终端报警 导出
     *
     * @param alarmVo
     * @param request
     * @param res
     */
    @Override
    public void terminalExport(AlarmVo alarmVo, HttpServletRequest request,
                               HttpServletResponse res) throws Exception {
        alarmVo.setPageNum(Constants.EXPORT_PAGE_NUM);
        alarmVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
        PageExtend<AlarmInfoResDto> alarmInfoDtoPageExtend = listPageAlarmInfo(alarmVo);

        final String[] propNames = {"vin", "license", "vehicleModelStr", "alarmName", "level", "beginTime"};
        final String[] headStrs = {"车架号", "车牌号", "车型", "报警类型", "报警等级", "报警状态", "报警时间"};
        String title = "车载终端报警";
        ExcelUtil.exportDataExcel(request, res, alarmInfoDtoPageExtend.getRecords(), propNames, headStrs, title);
    }

    /**
     * 电子围栏报警下载
     *
     * @param AlarmVo
     * @param request
     * @param res
     * @throws Exception
     */
    @Override
    public void fenceExport(AlarmVo AlarmVo, HttpServletRequest request,
                            HttpServletResponse res) throws Exception {
        AlarmVo.setPageNum(Constants.EXPORT_PAGE_NUM);
        AlarmVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
        PageExtend<AlarmInfoResDto> alarmInfoDtoPageExtend = listPageAlarmInfo(AlarmVo);

        final String[] propNames = {"vin", "license", "vehicleModelStr", "alarmName", "memo", "statusName", "beginTime"};
        final String[] headStrs = {"车架号", "车牌号", "车型", "报警类型", "报警内容", "报警状态", "报警时间"};
        String title = "电子围栏报警";
        ExcelUtil.exportDataExcel(request, res, alarmInfoDtoPageExtend.getRecords(), propNames, headStrs, title);
    }

    /**
     * 里程异变报警 列表
     *
     * @param alarmVo
     * @return
     */
    @Override
    public PageExtend<AlarmInfoResDto> listMileageException(AlarmVo alarmVo) {

        AlarmInfoReqDto alarmInfoReqDto = convertToAlarmInfoReqDto(alarmVo);
        alarmInfoReqDto.setAlarmCode(AlarmCodeEnum.MILEAGE_EXCEPTION_CHANGE.getFlag());

        log.info("里程异变报警 列表 AlarmInfoDto入参:{}", alarmInfoReqDto);
        return alarmRemoteService.listPageAlarmInfo(alarmInfoReqDto);
    }

    /**
     * 里程异变报警 导出
     *
     * @param alarmVo
     * @param request
     * @param res
     */
    @Override
    public void mileageExceptionExport(AlarmVo alarmVo, HttpServletRequest request,
                                       HttpServletResponse res) throws Exception {

        alarmVo.setPageNum(Constants.EXPORT_PAGE_NUM);
        alarmVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
        PageExtend<AlarmInfoResDto> alarmInfoDtoPageExtend = listMileageException(alarmVo);

        final String[] propNames = {"vin", "license", "vehicleModelStr", "alarmName", "memo", "differenceMileage", "statusName",
                "meAuditStatusStr", "beginTime"};
        final String[] headStrs = {"车架号", "车牌号", "车型", "报警类型", "报警内容", "异变里程", "报警状态", "处理意见", "报警时间"};
        String title = "里程异变报警";
        ExcelUtil.exportDataExcel(request, res, alarmInfoDtoPageExtend.getRecords(), propNames, headStrs, title);
    }

    /**
     * 里程异变处理 审核
     *
     * @param alarmVo
     */
    @Override
    public RestResponse meAudit(AlarmVo alarmVo) {

        AlarmMeAuditReqDto alarmMeAuditReqDto = new AlarmMeAuditReqDto();
        CommonUtils.copyProperties(alarmVo, alarmMeAuditReqDto);

        return alarmRemoteService.meAudit(alarmMeAuditReqDto);
    }

    @Override
    public RestResponse alarmHandling(AlarmHandlingVo vo) {
        AlarmHandlingDto dto = new AlarmHandlingDto();
        CommonUtils.copyProperties(vo, dto);
        return alarmRemoteService.alarmHandling(dto);
    }

    @Override
    public RestResponse getAlarmInfoHandlerRecordList(AlarmInfoHandlerRecordReqVo vo) {
        AlarmInfoHandlerRecordReqDto dto = new AlarmInfoHandlerRecordReqDto();
        CommonUtils.copyProperties(vo, dto);
        return alarmRemoteService.getAlarmInfoHandlerRecordList(dto);
    }

    /**
     * 转换为
     *
     * @param alarmVo
     * @return
     */
    private AlarmInfoReqDto convertToAlarmInfoReqDto(AlarmVo alarmVo) {
        AlarmInfoReqDto alarmInfoReqDto = new AlarmInfoReqDto();
        CommonUtils.copyProperties(alarmVo, alarmInfoReqDto);
        return alarmInfoReqDto;
    }
}
